using Demo02;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.Configure<BookOption>(x => x.Name = "abcdef");


builder.Services.AddSwaggerGen(x =>
{
 x.SwaggerDoc("v1",new OpenApiInfo()
 {
      Contact = new OpenApiContact(){Email = "273168121@qq.com"},
      Description = "测试",
      Title = "测试标题",
      Version = "v"
 });
 x.IncludeXmlComments(System.IO.Path.Combine(System.AppContext.BaseDirectory, "Demo02.xml"));
});
//builder.Services.AddSingleton<IConfigureOptions<BookOption>, BookOptionConfigure>();
builder.Services.Configure<ApiBehaviorOptions>(x => x.InvalidModelStateResponseFactory = (context) =>
{
    var rs = new
    {
        url=context.HttpContext.Request.GetDisplayUrl(),
        type=context.GetType().ToString(),
        mode=context is ActionExecutingContext,
        error = context.ModelState.ErrorCount,
        msg=context.ActionDescriptor.DisplayName,
        Controller=(context as ActionExecutingContext)?.Controller.GetType().ToString()
    };
    context.HttpContext.Response.Cookies.Append("test","1233");
    return new ObjectResult(rs) { StatusCode = 400};
});


var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();